package com.haixiaoke.saas.enterprise.cleaner.controller;

import javax.servlet.http.HttpServletResponse;

import com.haixiaoke.saas.cleaner.domain.Cleaner;
import com.haixiaoke.saas.cleaner.service.ICleanerService;
import com.haixiaoke.saas.common.annotation.RepeatSubmit;
import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 保洁人员信息Controller
 *
 * @author xiaolong
 * @date 2024-05-08
 */
@RestController
@RequestMapping("/enterprise/cleaner/cleaner")
public class EnterpriseCleanerController extends BaseController {
    @Autowired
    private ICleanerService cleanerService;

    /**
     * 查询保洁人员信息列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:cleaner:list')")
    @GetMapping("/list")
    @TenantScope
    public TableDataInfo list(Cleaner cleaner) {
        startPage();
        cleaner.setTenantId(TenantContext.getTenantId());
        List<Cleaner> list = cleanerService.selectCleanerList(cleaner);
        return getDataTable(list);
    }

    /**
     * 导出保洁人员信息列表
     */
    @PostMapping("/export")
    @TenantScope
    @RepeatSubmit
    public void export(HttpServletResponse response, Cleaner cleaner) {
        cleaner.setTenantId(TenantContext.getTenantId());
        List<Cleaner> list = cleanerService.selectCleanerList(cleaner);
        ExcelUtil<Cleaner> util = new ExcelUtil<Cleaner>(Cleaner.class);
        util.exportExcel(response, list, "保洁人员信息数据");
    }

    /**
     * 获取保洁人员信息详细信息
     */
    @GetMapping(value = "/{cleanerId}")
    public AjaxResult getInfo(@PathVariable("cleanerId") Long cleanerId) {
        return success(cleanerService.selectCleanerByCleanerId(cleanerId));
    }

    /**
     * 新增保洁人员信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:cleaner:add')")
    @PostMapping
    @TenantScope
    public AjaxResult add(@RequestBody Cleaner cleaner) {
        Long userId = getUserId();
        cleaner.setTenantId(TenantContext.getTenantId());
        cleaner.setOperatorId(userId);
        return toAjax(cleanerService.insertCleaner(cleaner));
    }

    /**
     * 修改保洁人员信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:cleaner:edit')")
    @PutMapping
    @TenantScope
    @RepeatSubmit
    public AjaxResult edit(@RequestBody Cleaner cleaner) {
        Long userId = getUserId();
        cleaner.setOperatorId(userId);
        return toAjax(cleanerService.updateCleaner(cleaner));
    }

    /**
     * 删除保洁人员信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:cleaner:remove')")
    @DeleteMapping("/{cleanerIds}")
    @TenantScope
    @RepeatSubmit
    public AjaxResult remove(@PathVariable Long[] cleanerIds) {
        return toAjax(cleanerService.deleteCleanerByCleanerIds(cleanerIds));
    }


    /**
     * 获取企业用户信息
     */
    @GetMapping(value = "/userInfo")
    @TenantScope
    public AjaxResult getUserInfo() {
        return success(cleanerService.getUserInfo());
    }


    /**
     * 查询验房人员信息
     */
    @GetMapping(value = "/cleanerUserInfo")
    @TenantScope
    public AjaxResult cleanerUserInfo(Cleaner cleaner) {
        cleaner.setTenantId(TenantContext.getTenantId());
        return success(cleanerService.cleanerUserInfo(cleaner));
    }
}
